//
//  LYAlliyunUpLoadManager.m
//  O Chat
//
//  Created by 涂阳 on 2019/8/13.
//  Copyright © 2019 ly. All rights reserved.
//

#import "TYAlliyunUpLoadManager.h"
#import <VODUpload/VODUploadClient.h>
@interface TYAlliyunUpLoadManager ()
@property (nonatomic , strong)VODUploadClient *uploader;
@end

@implementation TYAlliyunUpLoadManager
+(TYAlliyunUpLoadManager *)shareManager{
    static TYAlliyunUpLoadManager *sharedGlobalInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedGlobalInstance = [[self alloc] init];
    });
    return sharedGlobalInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initWithConfig];
    }
    return self;
}

- (void)initWithConfig {
    _uploader = [VODUploadClient new];
    __weak VODUploadClient *weakClient = _uploader;
    __weak typeof(self) weakSelf = self;
    // 完成
    OnUploadFinishedListener FinishCallbackFunc = ^(UploadFileInfo* fileInfo,  VodUploadResult* result){
        NSLog(@"upload finished callback videoid:%@, imageurl:%@", result.videoId, result.imageUrl);
        
        if (weakSelf.uploadFinishBlock) {
            weakSelf.uploadFinishBlock(weakSelf.videoId);
        }
    };
    
    // 失败
    OnUploadFailedListener FailedCallbackFunc = ^(UploadFileInfo* fileInfo, NSString *code, NSString* message){
        NSLog(@"upload failed callback code = %@, error message = %@", code, message);
        if (weakSelf.uploadFinishBlock){
            weakSelf.upLoadFailBlock();
        }
    };
    
    // 进度
    OnUploadProgressListener ProgressCallbackFunc = ^(UploadFileInfo* fileInfo, long uploadedSize, long totalSize) {
        if (weakSelf.uploadProgressBlock) {
            
            weakSelf.uploadProgressBlock(uploadedSize/totalSize);
        }
    };
    
    // token过期
    OnUploadTokenExpiredListener TokenExpiredCallbackFunc = ^{
        NSLog(@"upload token expired callback.");
        
        [weakSelf getNewUploadAuth];
        // token过期，设置新的STS，继续上传
        //        [weakSelf.uploader resumeWithToken:`STS Key Id` accessKeySecret:`STS Key Secret` secretToken:`STS Secret Token` expireTime:`STS Expire Time`];
    };
    
    // 上传开始重试回调
    OnUploadRertyListener RetryCallbackFunc = ^{
        NSLog(@"upload retry begin callback.");
    };
    //上传结束重试，继续上传回调
    OnUploadRertyResumeListener RetryResumeCallbackFunc = ^{
        NSLog(@"upload retry end callback.");
    };
    
    //  开始上传回调
    OnUploadStartedListener UploadStartedCallbackFunc = ^(UploadFileInfo* fileInfo) {
        NSLog(@"upload upload started callback.");
        // 设置上传地址和凭证
        [weakClient setUploadAuthAndAddress:fileInfo uploadAuth:weakSelf.uploadAuth uploadAddress:weakSelf.uploadAddress];
    };
    
    // 回调的监听
    VODUploadListener *listener = [[VODUploadListener alloc] init];
    listener.finish = FinishCallbackFunc;
    listener.failure = FailedCallbackFunc;
    listener.progress = ProgressCallbackFunc;
    listener.expire = TokenExpiredCallbackFunc;
    listener.retry = RetryCallbackFunc;
    listener.retryResume = RetryResumeCallbackFunc;
    listener.started = UploadStartedCallbackFunc;
    // init with upload address and upload auth
    [self.uploader init:listener];
}

#pragma mark - 获取最新的上传凭证
- (void)getNewUploadAuth {
    [self.uploader resumeWithAuth:self.uploadAuth];
}



- (void)uploadVideoWithFilePath:(NSString *)filePath andChallengeType:(NSString *)title andcateId:(int)Id andDesc:(NSString *)desc andcoverUrl:(NSString *)cover{
    VodInfo *vodInfo = [[VodInfo alloc] init];
    vodInfo.title = title;
    vodInfo.desc = desc;
    if (cover.length > 0){
        vodInfo.coverUrl = cover;
    }
    //    vodInfo.cateId = [NSNumber numberWithInteger:challengeModel.itemId.integerValue];
    vodInfo.cateId = @(Id);
    vodInfo.tags = @"";
    [self.uploader addFile:filePath vodInfo:vodInfo];
    [self startUpload];
}


- (void)startUpload {
    [self.uploader start];
}

- (void)stopUpload {
    [self.uploader stop];
}

@end
